package com.zzyl.job;

import cn.hutool.json.JSONUtil;
import com.zzyl.service.AlertRuleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.AlertRuleVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AlertRuleJob {

    @Autowired
    private AlertRuleService alertRuleService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Scheduled(cron = "0 * * * * ?")
    public void alertRuleJob(){
        //查询数据规则
        List<AlertRuleVo> alertRule = alertRuleService.getAlertRule(null,null,null); //**
        if (ObjectUtil.isEmpty(alertRule)){
            return;
        }
        //查询redis数据
        List<Object> values = redisTemplate.opsForHash().values("devicedata");
        if (ObjectUtil.isEmpty(values)){
            return;
        }
        //redis 数据做标准化处理
        List<DeviceDataVo> alldeviceDataVoList = new ArrayList<>();
        values.forEach(v -> {
            List<DeviceDataVo> deviceDataVoList = JSONUtil.toList(v.toString(), DeviceDataVo.class);
            alldeviceDataVoList.addAll(deviceDataVoList);
        });
        //报警数据单个处理匹配规则
        alldeviceDataVoList.forEach(deviceDataVo -> {
            alertRuleService.alertRuleHandler(deviceDataVo);
        });
    }

}


















